//
//  MailboxBindingViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "MailboxBindingViewController.h"
#import "CEAccessoryTextField.h"

@interface MailboxBindingViewController ()<UITextFieldDelegate>

/** 解绑email */
@property(nonatomic, strong) CEAccessoryTextField *emailTextField;

@end

@implementation MailboxBindingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"邮箱解绑";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupConstraints];
}

#pragma mark - Constraints

- (void)setupConstraints {
    
    [self.emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@50);
    }];
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
    self.navigationItem.rightBarButtonItem = saveItem;

}

- (void)saveAction {
    
    if (self.emailTextField.text.length == 0) {
        ShowMessage(@"请输入新的邮箱");
        return;
    }
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Put hostType:MLAHttpHostType_Api api:[self getUserIdApi:@"/api/v1/users"] params:@{@"email":self.emailTextField.text}.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [UserManager sharedInstance].userModel.value.email = self.emailTextField.text;
        ShowMessage(@"修改成功");
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        ShowMessage(@"修改失败,请重新修改");
    }];
}


- (CEAccessoryTextField *)emailTextField {
    
    if (!_emailTextField) {
        _emailTextField = [[CEAccessoryTextField alloc] init];
        _emailTextField.type = CEAccessoryTextFieldTypeLabel;
        _emailTextField.titleLabel.text = @"  邮箱：";
        _emailTextField.placeholder = [UserManager sharedInstance].userModel.value.email;
        _emailTextField.delegate = self;
        _emailTextField.backgroundColor = [UIColor whiteColor];
        _emailTextField.underlineView.hidden = YES;
        [self.view addSubview:_emailTextField];
    }
    return _emailTextField;
    
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIControl

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignAllTextFieldResponder];
}

#pragma mark - Private

- (void)resignAllTextFieldResponder {
    
    [self.emailTextField resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
